package se.teamstat.client.gui.admin.player;

import se.teamstat.client.dto.EntityDTO;
import se.teamstat.client.gui.admin.EntityPanel;
import se.teamstat.client.service.MainServiceAsync;
import se.teamstat.client.service.ServiceFactory;
import se.teamstat.client.service.ServiceFactoryImpl;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;

public abstract class AddPanel extends FormPanel {
	
	private ServiceFactory serviceFactory;
	
	public AddPanel() {
		serviceFactory = new ServiceFactoryImpl();
	}
	
	final AsyncCallback<EntityDTO> callback = new AsyncCallback<EntityDTO>() {
		public void onSuccess(EntityDTO entity) {
			MessageBox.alert("success:" + entity);
		}

		public void onFailure(Throwable caught) {
			MessageBox.alert("failed");
		}
	};
	
	ButtonListenerAdapter listener = new ButtonListenerAdapter() {
		public void onClick(Button button, EventObject e) {
			EntityPanel activePanel = (EntityPanel) getActiveTab();
			EntityDTO entity = activePanel.getEntity();
			getService(entity.getClass()).save(entity, callback);
		}
	};
	
	public MainServiceAsync<EntityDTO> getService(Class<? extends EntityDTO> entity) {
		return serviceFactory.getService(entity);
	}
	
	public abstract EntityPanel getActiveTab();
	
}
